<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\News as NewsModel;
use app\common\model\NewsCate;
use app\common\model\NewsComment;
use app\common\model\NewsLike;
use app\common\validate\NewsComment as NewsCommentValidate;


class News extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function index(){
        $lists = (new NewsModel())->where([
            'news_cate_id' => ['not in','6,7,8'],
            'cate.is_sys' => 0
        ])->with(['teacher','cate'])->limit(0,3)->select();
        foreach ($lists as $k=>$v){
            $v['content'] = preg_replace('/&[a-zA-Z0-9#]+;/', '', $v['content']);
            $v['content'] = mb_substr(strip_tags($v['content']), 0, 50, "UTF-8");
        }

        $data['lists'] = $lists;
        $cateList = (new NewsCate())->limit(0,4)->order('id asc')->select();
        $cates = [];
        foreach ($cateList as $k=>$v){
            $cates[] = $v['name'];
        }
        $data['cates'] = $cates;

        $data['rule_id'] = NewsModel::where([
            'news_cate_id'=> 6
        ])->value('id');

        $data['agreement_id'] = NewsModel::where([
            'news_cate_id'=> 7
        ])->value('id');

        $this->success('success', $data);
    }

    public function lists()
    {
        $params = $this->request->param();
        $where = [
            'news_cate_id' => $params['cate_id'],
        ];

        $lists = NewsModel::where($where)->with('teacher')->paginate(10);
        foreach ($lists as $k=>$v){
            $v['content'] = preg_replace('/&[a-zA-Z0-9#]+;/', '', $v['content']);
            $v['content'] = mb_substr(strip_tags($v['content']), 0, 50, "UTF-8");
        }
        $result['data'] = $lists;

        $result['cate_name'] = NewsCate::where(['id'=>$params['cate_id']])->value('name');
        $this->success('success', $result);
    }

    public function detail()
    {
        $params = $this->request->param();
        $user_id = $this->auth->id;
        $where = [
            'id' => $params['id'],
        ];
        $data = NewsModel::where($where)->find();

        $data['is_sys'] = (new NewsCate())->where([
            'id'=> $data['news_cate_id']
        ])->value('is_sys');

        $data['comment'] = (new NewsComment())->with('user')->where([
            'news_id' => $params['id'],
        ])->select();
        $data['isLike'] = (new NewsLike())->where([
            'news_id' => $params['id'],
            'user_id' => $user_id
        ])->count();
        $this->success('success', $data);
    }

    //提交新闻评价
    public function comment()
    {
        $param = $this->request->post();
        $user_id = $this->auth->id;
        $param['user_id'] = $user_id;

        $validate = new NewsCommentValidate();
        if (!$validate->scene('add')->check($param)) {
            $this->error($validate->getError());
        }

        $commentModel = new NewsComment();
        $commentModel->save([
            'user_id' => $user_id,
            'news_id' => $param['news_id'],
            'content' => $param['content']
        ]);

        $this->success('评论成功');
    }

    //点赞
    public function like()
    {
        $param = $this->request->post();
        $user_id = $this->auth->id;
        $NewsLikeModel = new NewsLike();
        $find = $NewsLikeModel->where([
            'user_id' => $user_id,
            'news_id' => $param['news_id']
        ])->find();
        if ($find) {
            $NewsLikeModel->where([
                'user_id' => $user_id,
                'news_id' => $param['news_id']
            ])->delete();
            $this->success('取消点赞',0);
        }else{
            $NewsLikeModel::create([
                'user_id' => $user_id,
                'news_id' => $param['news_id']
            ]);
            $this->success('点赞',1);
        }

    }
}